﻿using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace Devtm.ServiceModel.Discovery.Collection
{



    /// <summary>
    /// Implement pattern Iterator
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="TKey">The type of the key.</typeparam>
    [DebuggerNonUserCodeAttribute()]
    public class Enumerator<T, TKey> : IEnumerator<T>
    {

        private Hashtable hashtable;
        private List<TKey> bucketkey = new List<TKey>();
        private T _current;
        private int bucket;
        private int lenght;
        private TKey currentKey;


        public Enumerator(Hashtable hashtable, List<TKey> bucketkey)
        {
            this.hashtable = hashtable;
            this.bucketkey = bucketkey;
            this.bucket = 0;
        }


        public virtual bool MoveNext()
        {

            if (bucket == 0)
                Reset();

            while (this.bucket < this.lenght)
            {
                this.currentKey = this.bucketkey[this.bucket];
                if (this.currentKey == null)
                    return false;

                _current = (T)this.hashtable[currentKey];
                this.bucket++;

                return true;
            }
            return false;

        }


        public virtual void Reset()
        {
            this.lenght = this.bucketkey.Count;
        }


        public virtual T Current { get { return _current; } }

        object IEnumerator.Current { get { return _current; } }

        public void Dispose()
        {
            this.bucket = 0;
        }

    }

}
